package com.lixingqu.mvpdemo.ui.login;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.lixingqu.mvpdemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class LoginActivity extends Activity implements ILoginView {
    @BindView(R.id.et_name)
    EditText et_name;
    @BindView(R.id.et_password)
    EditText et_password;
    @BindView(R.id.btn_login)
    Button btn_login;
    @BindView(R.id.btn_reset)
    Button btn_reset;
    @BindView(R.id.pb)
    ProgressBar pb;
    LoginPresenterImpl loginPresenter = new LoginPresenterImpl(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_login, R.id.btn_reset})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                loginPresenter.login();
                break;
            case R.id.btn_reset:
                loginPresenter.reset();
                rxjava();
                break;
            default:
                break;
        }
    }

    /**
     * testRxjava
     */
    private void rxjava() {
        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("1");
                L("1");
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io())
//                .observeOn(Schedulers.io())
                .map(new Func1<String, User>() {
                    @Override
                    public User call(String s) {
                        if (s.equals("2")) {
                            throw new RuntimeException("错误");
                        }
                        L("2");
                        return new User("zhangsan", s);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<User>() {
            @Override
            public void call(User user) {
                Log.i("lxq", user.toString());
                L("3");
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                Log.i("lxq", "error");
                L("4");
            }
        });
    }

    private void L() {
        Log.i("lxq", "线程名字：" + Thread.currentThread().getName());
    }

    private void L(String s) {
        Log.i("lxq", "位置" + s + "线程名字：" + Thread.currentThread().getName());
    }

    @Override
    public void onRequestStart() {
        pb.setVisibility(View.VISIBLE);
    }

    @Override
    public void onRequestEnd() {
        pb.setVisibility(View.GONE);
    }

    @Override
    public void onRequestError() {
        pb.setVisibility(View.GONE);
        Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public String getUsername() {
        return et_name.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return et_password.getText().toString().trim();
    }

    @Override
    public void reset() {
        et_name.setText("");
        et_password.setText("");
    }

    @Override
    public void loginSuccess(User user) {
        Toast.makeText(LoginActivity.this, user.toString(), Toast.LENGTH_SHORT).show();
    }


}
